package games.menem 
{
	import games.BaseGame;
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxRect;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxG;
	import org.flixel.FlxText;
	
	/**
	 * ...
	 * @author Jacowaco
	 */
	public class MenemGame extends BaseGame
	{
		private var menem:MenemChar;
		protected var asteroids:FlxGroup;
		public var bullets:FlxGroup;
		private var newAsteroidPos:int;
		private var gameWidth:int;
		
		override public function create():void 
		{
			super.create();
			
			FlxG.bgColor = 0xff000000;
			
			gameWidth = 10000;
			
			asteroids = new FlxGroup();
			bullets = new FlxGroup();
			
			add(asteroids);
			
			var sprite:FlxSprite;
			for(var i:int = 0; i < 20; i++)
			{
				sprite = new FlxSprite(-100,-100);
				sprite.makeGraphic(6,2);
				sprite.exists = false;
				bullets.add(sprite);
			}
			add(bullets);
			
			newAsteroidPos = 400;
			
			while (newAsteroidPos < gameWidth)
			{
				var a:Asteroid = new Asteroid(newAsteroidPos, Math.random() * FlxG.height);
				asteroids.add(a);
				
				newAsteroidPos += 60;
			}
			
			menem = new MenemChar(60, FlxG.height / 2);
			add(menem);
			
			FlxG.worldBounds = new FlxRect(0, 0, gameWidth, FlxG.height);
			
			FlxG.camera.follow(menem);
			FlxG.camera.deadzone = new FlxRect(0, 0, 150, FlxG.height);
			FlxG.camera.bounds = new FlxRect(0, 0, gameWidth, FlxG.height);
		}
		
		override public function update():void 
		{
			super.update();
			
			if (gameState == "playing")
			{
				if (menem.x > gameWidth)
				{
					endGame(true);
				}
				else
				{
					FlxG.overlap(menem, asteroids, menemCollidedAsteroid);
					FlxG.overlap(bullets, asteroids, bulletsCollideAsteroids);
				}
			}
		}
		
		public function menemCollidedAsteroid(obj1:FlxObject,obj2:FlxObject):void
		{
			menem.hurt(1);
			obj2.kill();
			
			if (menem.health == 0)
			{
				endGame(false);
			}
		}
		
		public function bulletsCollideAsteroids(obj1:FlxObject,obj2:FlxObject):void
		{
			obj1.kill();
			obj2.kill();
		}
		
		override protected function onReplay():void 
		{
			FlxG.switchState(new MenemGame());
		}
	}

}